package com.uglive.cg.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ChangKuModel {
    public final String SP_CHANGKU_DATA = "SP_CHANGKU_DATA";

    //保持仓库列表信息
    public void putCKToSP(List<String> data) {
        SPUtils.getInstance().put(SP_CHANGKU_DATA, new HashSet<>(data));
    }

    //获取厂库列表
    public List<String> getCKListFromSp() {
        return new ArrayList<>(SPUtils.getInstance().getStringSet(SP_CHANGKU_DATA));
    }

    public  void getCkList(ApiModel.OnFailure onFailure, ApiModel.OnSuccess onSuccess) {
        new ApiModel().changKuList(
                () -> {
                    List<String> localData = getCKListFromSp();
                    if (localData.isEmpty()){
                        onFailure.onFailure();
                    } else {
                       onSuccess.onSuccess(JSON.toJSONString(localData));
                    }
                }, data -> {
                    putCKToSP(JSON.parseArray(data,String.class));
                    onSuccess.onSuccess(data);
                });
    }

}
